package com.liuchen.api.controller;

import com.liuchen.boot.system.controller.BaseController;
import com.liuchen.common.util.PropertiesEnvUtils;
import com.liuchen.common.util.ReadFromFileUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 接受请求
 */
@RestController
@EnableAutoConfiguration
public class ApiController extends BaseController {

    private static String path;

    /**
     * 模拟的接口,url在api下
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/api/**")
    public Object api(HttpServletRequest request) throws Exception {
        //获取地址
        String uri = request.getRequestURI().replaceAll("api/", "").split("\\.")[0];
        if (path == null) {
            //获取本地json文件地址
            String jsonPath = PropertiesEnvUtils.getPropertiesString(null, "json.path");
            //获取项目运行的地址
            String springRoot = ClassUtils.getDefaultClassLoader().getResource("").getPath().replaceAll("/target/classes/", "");
            //地址
            path = jsonPath.replaceAll(" ", "").replaceAll("\\{spring-boot\\}", springRoot);
        }
        String file = path + uri + ".json";
        //读取文件内容
        String dataJson = ReadFromFileUtils.readFileByLines(file);
        //判断内容
        if ("".equals(dataJson)) {
            Map<String, String> map = new HashMap();
            map.put("desc", "获取本地返回JSON异常");
            map.put("path", file);
            map.put("info", "检查本地文件,或者使用接口修改本地json文件路径");
            return map;
        }
        return dataJson;
    }

    /**
     * 获取当前地址
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getPath")
    public Object getPath(HttpServletRequest request) throws Exception {
        if (path == null) {
            //获取本地json文件地址
            String jsonPath = PropertiesEnvUtils.getPropertiesString(null, "json.path");
            //获取项目运行的地址
            String springRoot = ClassUtils.getDefaultClassLoader().getResource("").getPath().replaceAll("/target/classes/", "");
            //地址
            path = jsonPath.replaceAll(" ", "").replaceAll("\\{spring-boot\\}", springRoot);
        }
        Map<String, String> map = new HashMap();
        map.put("desc", "当前使用的JSON地址");
        map.put("path", path);
        return map;
    }

    /**
     * 修改地址
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/setPath")
    public Object setPath(HttpServletRequest request) throws Exception {
        String newPath = request.getParameter("path").replaceAll(" ", "");
        if (newPath.endsWith("/")) {
            newPath = newPath.substring(0, newPath.length() - 1);
        }
        path = newPath;
        Map<String, String> map = new HashMap();
        map.put("desc", "已修改地址");
        map.put("path", path);
        return map;
    }

} 